//
//  L0PlugSuitTable.m
//  PlugSuit
//
//  Created by ∞ on 24/10/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "L0PlugSuitTable.h"


@implementation L0PlugSuitTable

- (id) initWithFrame:(NSRect) frame {
	if (self = [super initWithFrame:frame])
		[self awakeFromInit];
	
	return self;
}

- (id) initWithCoder:(NSCoder*) coder {
	if (self = [super initWithCoder:coder])
		[self awakeFromInit];
	
	return self;
}

- (void) awakeFromInit {
	NSLog(@"wow");
	[self registerForDraggedTypes:
		[NSArray arrayWithObject:NSFilenamesPboardType]];
	
	[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(_didFinishCopyingFiles:) name:NSWorkspaceDidPerformFileOperationNotification object:nil];
}

- (void) _didFinishCopyingFiles:notif {
	[_pane _updateRegisteredPlugIns];
}

- (NSDragOperation) draggingEntered:(id <NSDraggingInfo>) sender {
	if ([[[sender draggingPasteboard] types] containsObject:NSFilenamesPboardType]) {
		_dragging = YES;
		[self setNeedsDisplay];
		return NSDragOperationCopy;
	} else
		return NSDragOperationNone;
}

- (NSDragOperation) draggingUpdated:(id <NSDraggingInfo>)sender {
	return [[[sender draggingPasteboard] types] containsObject:NSFilenamesPboardType]? NSDragOperationCopy : NSDragOperationNone;
}

- (void) draggingExited:(id <NSDraggingInfo>) sender {
	_dragging = NO;
	[self setNeedsDisplay];
}

- (BOOL) prepareForDragOperation:(id <NSDraggingInfo>) sender {
	return [[[sender draggingPasteboard] types] containsObject:NSFilenamesPboardType];
}

- (BOOL) performDragOperation:(id <NSDraggingInfo>) sender {
	BOOL result = NO;
	if (![[[sender draggingPasteboard] types] containsObject:NSFilenamesPboardType])
		goto returnVal;
	
	NSString* dest;
	if (!(dest = [self _createASDirectoryForDrop])) {
		NSBeep();
		goto returnVal;
	}
	
	NSArray* files = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType];
	NSWorkspace* ws = [NSWorkspace sharedWorkspace];
		
	NSEnumerator* enu = [files objectEnumerator];
	NSString* file;
	
	while (file = [enu nextObject]) {
		int tag = 0;
		[ws performFileOperation:NSWorkspaceCopyOperation source:[file stringByDeletingLastPathComponent] destination:dest files:[NSArray arrayWithObject:[file lastPathComponent]] tag:&tag];
		
		if (tag < 0) {
			NSBeep();
			goto returnVal;
		}
	}
	
	result = YES;
	
returnVal:
	_dragging = NO;
	[self setNeedsDisplay];
	return result;
}

- (NSString*) _createASDirectoryForDrop {
	NSArray* libs = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
	
	if ([libs count] == 0)
		return nil;
	
	NSString* library = [libs objectAtIndex:0];
	NSFileManager* fm = [NSFileManager defaultManager];
	
	NSString* as = [library stringByAppendingPathComponent:@"Application Support"];
	if (![fm fileExistsAtPath:as] && ![fm createDirectoryAtPath:as attributes:nil])
		return nil;
	
	NSString* simbl = [as stringByAppendingPathComponent:@"SIMBL"];
	if (![fm fileExistsAtPath:simbl] && ![fm createDirectoryAtPath:simbl attributes:nil])
		return nil;
	
	NSString* plugins = [simbl stringByAppendingPathComponent:@"Plugins"];
	if (![fm fileExistsAtPath:plugins] && ![fm createDirectoryAtPath:plugins attributes:nil])
		return nil;
	else
		return plugins;
}

- (void) drawRect:(NSRect) rect {
	[super drawRect:rect];
	
	if (_dragging) {
		[[NSColor selectedControlColor] set];
		NSFrameRectWithWidth([self frame], 2.0);
	}
}

@end
